package com.wkbb.basic.controller.open;

import com.wkbb.basic.model.Blacklist;
import com.wkbb.basic.service.BlackListService;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.BlacklistInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangdaofang
 * 创建时间：2019-09-21 17:28
 */
@RestController
@Api(description = "黑名单模块对外接口")
@RequestMapping("/basic/{apiVersion}/open")
@Slf4j
public class BlacklistController {
    @Autowired
    private BlackListService blackListService;


    @ApiOperation(value = "根据用户ID获取黑名单信息")
    @RequestMapping(value = "/getBlacklistByUserId", method = RequestMethod.GET)
    public ResultDTO<BlacklistInfoDto> getBlacklistByUserId(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "用户ID", required = true) @RequestParam(name = "userId", required = true) Long userId
    ){
        log.info("***********BlcaklistController.getBlacklistByUserId******apiVersion:{},orgId:{},userId:{}", apiVersion, orgId, userId);
        try {
            if (null == orgId || null == userId ) {return new ResultDTO<>(ErrorCode.PARAM_MISSING);}
            Map<String,Object> params = new HashMap<>();
            params.put("userId",userId);
            params.put("orgId",orgId);
            Blacklist blacklist = blackListService.getByMapParams(params);
            if(blacklist == null){
                return new ResultDTO<>();
            }
            BlacklistInfoDto blacklistDto = new BlacklistInfoDto();
            BeanUtils.copyProperties(blacklist, blacklistDto);
            return new ResultDTO<>(blacklistDto);
        } catch (Exception e) {
            log.error("***********BlcaklistController.getBlacklistByUserId******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation(value = "根据组织ID获取黑名单信息集合")
    @RequestMapping(value = "/getBlacklistByOrgIdList", method = RequestMethod.GET)
    public ResultDTO<List<BlacklistInfoDto>> getBlacklistByOrgIdList(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId
    ){
        log.info("***********BlcaklistController.getBlacklistByOrgIdList******apiVersion:{},orgId:{}", apiVersion, orgId);
        try {
            if (null == orgId ) {return new ResultDTO<>(ErrorCode.PARAM_MISSING);}
            Map<String,Object> params = new HashMap<>();
            params.put("orgId",orgId);
            return new ResultDTO<>( blackListService.getBlackListDtoByMapParams(params));
        } catch (Exception e) {
            log.error("***********BlcaklistController.getBlacklistByOrgIdList******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

    @ApiOperation("新增黑名单")
    @RequestMapping(value = "/addBlacklist", method = RequestMethod.POST)
    public ResultDTO<Object> addBlacklist(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "黑名单") @RequestBody BlacklistInfoDto blacklistInfoDto){

        log.info("***********BlacklistController.addBlacklist******...apiVersion:{},blacklistInfoDto:{}", apiVersion, blacklistInfoDto);

        if(blacklistInfoDto == null || blacklistInfoDto.getWarnLevel() == null ||
                blacklistInfoDto.getHeadImgUrl() == null){
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }

        try {
            return blackListService.insert(blacklistInfoDto,blacklistInfoDto.getUserName());
        } catch (Exception e) {
            log.error("***********BlacklistController.addBlacklist******新增黑名单异常", e);
            return new ResultDTO<>(false);
        }
    }
}
